import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBUtils/YBBaseUtils/YBCallPhoneUtils.dart';


class YBWebViewUtils extends StatefulWidget {

  final String typeStr;

  const YBWebViewUtils({Key key, this.typeStr}) : super(key: key);

  @override
  _YBWebViewUtilsState createState() => _YBWebViewUtilsState(typeStr: typeStr);
}

class _YBWebViewUtilsState extends State<YBWebViewUtils> {

  final String typeStr;

  String webStr;

  _YBWebViewUtilsState({this.typeStr});

  WebViewController _controller;

  String _titleStr = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (int.parse(typeStr) == 1) {
      //
      //http://m.ebs321.com/credit?from=app
      webStr = "http://m.ebs321.com/credit?from=app";
    } else if (int.parse(typeStr) == 2) {
      webStr = 'http://xin.ebs321.com?from=app';
    } else if (int.parse(typeStr) == 3) {
      webStr = 'https://app.ubzone.cn/share/support';
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    EasyLoading.dismiss();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Container(
      color: YBColor.white,
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          title: Text(_titleStr,style: YBTextStyle.TextStyle30313320Medium,),
        ),
        body:WebView(
          initialUrl: webStr,
          onPageFinished:(url){
            _controller.evaluateJavascript("document.title").then((value){
               setState(() {
                 _titleStr = value;
                 print("-----------$value--------");
               });
            });
            EasyLoading.dismiss();
          },
          onWebViewCreated: (controller){
             _controller = controller;
          },
          navigationDelegate:(NavigationRequest request){
            if (request.url.startsWith("js://webview?phone='4008-260-189'")) {
              print('-----------blocking navigation to $request}');
              YBCallPhoneUtils().getCallPhoneUtils("tel:4008-260-189");
              return NavigationDecision.prevent;
            }
            print('allowing navigation to $request');
            return NavigationDecision.navigate;
          },
          onPageStarted: (url){
            EasyLoading.show(status:"读取中...");
          },
          javascriptChannels: <JavascriptChannel>[
            JavascriptChannel(name: "commtjToast", onMessageReceived:(JavascriptMessage message){
              print("object--${message.message}---");
              YBCallPhoneUtils().getCallPhoneUtils("tel:${message.message}");
            }),
          ].toSet(),
          javascriptMode: JavascriptMode.unrestricted,
        ),
      ),
    );
  }
}